1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.util.concurrent;
18
19 import junit.framework.TestCase;
20
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.concurrent.Callable;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.Executors;
26 import java.util.concurrent.Future;
27 import java.util.concurrent.ScheduledExecutorService;
28 import java.util.concurrent.ScheduledFuture;
29 import java.util.concurrent.TimeUnit;
30 import java.util.concurrent.TimeoutException;
31
32
33
34
35
36
37 public class WrappingScheduledExecutorServiceTest extends TestCase {
38 private static final Runnable DO_NOTHING = new Runnable() {
39 @Override
40 public void run() {
41 }
42 };
43
44 public void testSchedule() {
45 MockExecutor mock = new MockExecutor();
46 TestExecutor testExecutor = new TestExecutor(mock);
47
48 testExecutor.schedule(DO_NOTHING, 10, TimeUnit.MINUTES);
49 mock.assertLastMethodCalled("scheduleRunnable", 10, TimeUnit.MINUTES);
50
51 testExecutor.schedule(Executors.callable(DO_NOTHING), 5, TimeUnit.SECONDS);
52 mock.assertLastMethodCalled("scheduleCallable", 5, TimeUnit.SECONDS);
53 }
54
55 public void testSchedule_repeating() {
56 MockExecutor mock = new MockExecutor();
57 TestExecutor testExecutor = new TestExecutor(mock);
58 testExecutor.scheduleWithFixedDelay(DO_NOTHING, 100, 10, TimeUnit.MINUTES);
59 mock.assertLastMethodCalled("scheduleWithFixedDelay", 100, 10, TimeUnit.MINUTES);
60
61 testExecutor.scheduleAtFixedRate(DO_NOTHING, 3, 7, TimeUnit.SECONDS);
62 mock.assertLastMethodCalled("scheduleAtFixedRate", 3, 7, TimeUnit.SECONDS);
63 }
64
65 private static final class WrappedCallable<T> implements Callable<T> {
66 private final Callable<T> delegate;
67
68 public WrappedCallable(Callable<T> delegate) {
69 this.delegate = delegate;
70 }
71
72 @Override
73 public T call() throws Exception {
74 return delegate.call();
75 }
76 }
77
78 private static final class WrappedRunnable implements Runnable {
79 private final Runnable delegate;
80
81 public WrappedRunnable(Runnable delegate) {
82 this.delegate = delegate;
83 }
84
85 @Override
86 public void run() {
87 delegate.run();
88 }
89 }
90
91 private static final class TestExecutor extends WrappingScheduledExecutorService {
92 public TestExecutor(MockExecutor mock) {
93 super(mock);
94 }
95
96 @Override
97 protected <T> Callable<T> wrapTask(Callable<T> callable) {
98 return new WrappedCallable<T>(callable);
99 }
100
101 @Override protected Runnable wrapTask(Runnable command) {
102 return new WrappedRunnable(command);
103 }
104 }
105
106 private static final class MockExecutor implements ScheduledExecutorService {
107 String lastMethodCalled = "";
108 long lastInitialDelay;
109 long lastDelay;
110 TimeUnit lastUnit;
111
112 void assertLastMethodCalled(String method, long delay, TimeUnit unit) {
113 assertEquals(method, lastMethodCalled);
114 assertEquals(delay, lastDelay);
115 assertEquals(unit, lastUnit);
116 }
117
118 void assertLastMethodCalled(String method, long initialDelay, long delay, TimeUnit unit) {
119 assertEquals(method, lastMethodCalled);
120 assertEquals(initialDelay, lastInitialDelay);
121 assertEquals(delay, lastDelay);
122 assertEquals(unit, lastUnit);
123 }
124
125 @Override public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
126 assertTrue(command instanceof WrappedRunnable);
127 lastMethodCalled = "scheduleRunnable";
128 lastDelay = delay;
129 lastUnit = unit;
130 return null;
131 }
132
133 @Override public <V> ScheduledFuture<V> schedule(
134 Callable<V> callable, long delay, TimeUnit unit) {
135 assertTrue(callable instanceof WrappedCallable);
136 lastMethodCalled = "scheduleCallable";
137 lastDelay = delay;
138 lastUnit = unit;
139 return null;
140 }
141
142 @Override public ScheduledFuture<?> scheduleAtFixedRate(
143 Runnable command, long initialDelay, long period, TimeUnit unit) {
144 assertTrue(command instanceof WrappedRunnable);
145 lastMethodCalled = "scheduleAtFixedRate";
146 lastInitialDelay = initialDelay;
147 lastDelay = period;
148 lastUnit = unit;
149 return null;
150 }
151
152 @Override public ScheduledFuture<?> scheduleWithFixedDelay(
153 Runnable command, long initialDelay, long delay, TimeUnit unit) {
154 assertTrue(command instanceof WrappedRunnable);
155 lastMethodCalled = "scheduleWithFixedDelay";
156 lastInitialDelay = initialDelay;
157 lastDelay = delay;
158 lastUnit = unit;
159 return null;
160 }
161
162
163 @Override
164 public boolean awaitTermination(long timeout, TimeUnit unit) {
165 throw new UnsupportedOperationException();
166 }
167
168 @Override
169 public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
170 throws InterruptedException {
171 throw new UnsupportedOperationException();
172 }
173
174 @Override
175 public <T> List<Future<T>> invokeAll(
176 Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
177 throws InterruptedException {
178 throw new UnsupportedOperationException();
179 }
180
181 @Override
182 public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
183 throws ExecutionException, InterruptedException {
184 throw new UnsupportedOperationException();
185 }
186
187 @Override
188 public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
189 throws ExecutionException, InterruptedException, TimeoutException {
190 throw new UnsupportedOperationException();
191 }
192
193 @Override
194 public boolean isShutdown() {
195 throw new UnsupportedOperationException();
196 }
197
198 @Override
199 public boolean isTerminated() {
200 throw new UnsupportedOperationException();
201 }
202
203 @Override
204 public void shutdown() {
205 throw new UnsupportedOperationException();
206 }
207
208 @Override
209 public List<Runnable> shutdownNow() {
210 throw new UnsupportedOperationException();
211 }
212
213 @Override
214 public <T> Future<T> submit(Callable<T> task) {
215 throw new UnsupportedOperationException();
216 }
217
218 @Override
219 public Future<?> submit(Runnable task) {
220 throw new UnsupportedOperationException();
221 }
222
223 @Override
224 public <T> Future<T> submit(Runnable task, T result) {
225 throw new UnsupportedOperationException();
226 }
227
228 @Override
229 public void execute(Runnable command) {
230 throw new UnsupportedOperationException();
231 }
232
233 }
234 }